package controllers;

import play.*;
import play.mvc.*;

import java.io.IOException;
import java.util.*;


import models.*;
import controlService.ControlService;
import controlService.TcardDB;

import java.util.Random;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.annotate.JsonTypeInfo.As;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class Application extends Controller {
	
    public static void index() {
    	Collection<Status> statuses = Status.GetAll();
    	int clientCount = ControlService.CS.getConnectedClientCount();
    	render(statuses, clientCount);
    }
    
    public static void populate() {
    	Collection<Tcard> allCards = TcardDB.DB.getAll();
    	
    	String json = "";
		try {
			json = Command.Mapper.writeValueAsString(allCards);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	renderJSON(json);
    }
    
    public static void checkForUpdates() {    	
    	List<Command> updates = ControlService.CS.getCommands();
    	
    	String json = "";
		try {
			json = Command.Mapper.writeValueAsString(updates);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    	renderJSON(json);
    }
    
    public static void stats() {
    	render();
    }
    
    public static void alterTcard()
    {
    	Collection<Tcard> allCards = TcardDB.DB.getAll();
    	Tcard testCard = allCards.iterator().next();
    	
    	testCard.setStatus(Status.OSRest); 	
    	
    	
    	renderText("Altered a tcard");    	
    }
    
    
    public static void makeMultipleTestCards()
    {	   	
    	Tcard card = Tcard.createTestCard(Kind.Engines, Status.Available, "Team Alpha",
    			"Type I", "LDW?!?!?", "Houston FD", "1", "Houston, TX", "Some Address", "10 minutes",
    			"Some other Address", "3 hours");
    	((EngineTcard)card).setPrimaryContactInfo("555-555-5555");
		TcardDB.DB.save(card); 
		
		card = Tcard.createTestCard(Kind.Engines, Status.Available, "Team Beta",
    			"Type III", "LDW?!?!?", "Houston FD", "2", "Houston, TX", "Some Address", "30 minutes",
    			"Some other Address", "3 hours");
		((EngineTcard)card).setPrimaryContactInfo("333-333-3333");
		TcardDB.DB.save(card);
		
		card = Tcard.createTestCard(Kind.Aircraft, Status.Assigned, "Air Team 1",
    			"C-130", "LDW?!?!?", "Houston FD", "234", "Dallas, TX", "Some Address", "45 minutes",
    			"Some other Address", "2 Hours");
		((AircraftTcard)card).setPilotName("Captain Airsmith");
		((AircraftTcard)card).setManufacturer("Lockheed");
		TcardDB.DB.save(card); 

		card = Tcard.createTestCard(Kind.HandCrews, Status.OSRest, "Team Beta",
    			"Hose Crew", "LDW?!?!?", "Houston FD", "43", "Houston, TX", "Some Address", "10 minutes",
    			"Some other Address", "1 hour"); 
		((HandCrewTcard)card).setManifest(Boolean.TRUE);
		((HandCrewTcard)card).setContactInfo("979-333-4444");
		((HandCrewTcard)card).setTravelMethod("Bus");
		((HandCrewTcard)card).setTransportationNeeds("Wheelchair");
		((HandCrewTcard)card).setTotalWeight(600);
		((HandCrewTcard)card).setLeaderName("Commander Smithy");
		TcardDB.DB.save(card); 

		card = Tcard.createTestCard(Kind.HeavyEquipment, Status.OSMechPers, "Team Beta",
    			"Crane", "LDW?!?!?", "Austin FD", "777", "Austin, TX", "Some Address", "10 minutes",
    			"Some other Address", "4 hours"); 
  	    TcardDB.DB.save(card); 

	  	card = Tcard.createTestCard(Kind.HandCrews, Status.Assigned, "Team Beta",
	  			"Hazmat Team", "LDW?!?!?", "Austin FD", "12", "Austin, TX", "Some Address", "10 minutes",
	  			"Some other Address", "2 hours"); 
		((HandCrewTcard)card).setContactInfo("512-666-4444");
		((HandCrewTcard)card).setTravelMethod("Car");
		((HandCrewTcard)card).setTransportationNeeds("Car");
		((HandCrewTcard)card).setTotalWeight(900);
		((HandCrewTcard)card).setLeaderName("Commander Johnson");
		TcardDB.DB.save(card); 

		card = Tcard.createTestCard(Kind.Helicopters, Status.Available, "Team Gamma",
    			"Blackhawk", "LDW?!?!?", "Austin FD", "777", "Austin, TX", "Some Address", "10 minutes",
    			"Some other Address", "4 hours"); 
		((HelicopterTcard)card).setPilotName("Captain Smith");
		TcardDB.DB.save(card); 

		card = Tcard.createTestCard(Kind.Personnel, Status.Available, "Team Alpha",
    			"Specialist", "LDW?!?!?", "Austin FD", "777", "Austin, TX", "Some Address", "10 minutes",
    			"Some other Address", "4 hours"); 
		((PersonnelTcard)card).setContactInfo("512-888-4444");
		((PersonnelTcard)card).setTravelMethod("Car");
		((PersonnelTcard)card).setTransportationNeeds("Car");
		((PersonnelTcard)card).setTotalWeight(200);
		((PersonnelTcard)card).setPosition("Explosives Technician");
		((PersonnelTcard)card).setPosition("Johan Von Smitherson");
		TcardDB.DB.save(card); 

		card = Tcard.createTestCard(Kind.Misc, Status.Assigned, "Security",
    			"Police", "LDW?!?!?", "HPD", "99", "Houston, TX", "Some Address", "In Transit",
    			"Some other Address", "30 Mintues"); 
		((MiscTcard)card).setLeaderName("Sargent John");
		TcardDB.DB.save(card); 
    }

}
